﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DAL;
using System.Xml.Linq;

public partial class admin_Banners_List : System.Web.UI.Page
{
  BannerBLL bannerData = new BannerBLL();  
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      Session["url_back_admin"] = Request.RawUrl;

      GetPositon();
      LoadPosition();
      ShowData();
    }
  }
  public struct Position
  {
    public string Id;
    public string Name;
    public Position(string id, string name)
    {
      Id = id;
      Name = name;
    }
  }
  public List<Position> listPosition = new List<Position>();
  public void GetPositon()
  {
    if (listPosition != null) listPosition.Clear();
    XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.xml");
    var xPosition = xElement.Descendants("Banners").Descendants("Position");
    foreach (XElement item in xPosition)
    {
      Position position = new Position(item.Attribute("Id").Value, item.Element("Name").Value);
      listPosition.Add(position);
    }
  }
  private void LoadPosition()
  {
    dropPosition.Items.Clear();
    foreach (Position item in listPosition)
    {
      dropPosition.Items.Add(new ListItem(item.Name, item.Id));
    }
  }
  private void ShowData()
  {
    int position = -1;
    int PageRow = Convert.ToInt32(dropPageSize.SelectedValue);
    gridView.PageSize = PageRow;
    if(dropPosition.Items.Count > 0)
      position = Convert.ToInt32(dropPosition.SelectedValue);

    if (dropIsEnable.SelectedIndex == 0)
    {
      if (position != -1)
        gridView.DataSource = bannerData.GetByPosition(position);
      else
        gridView.DataSource = bannerData.GetAll();
    }
    else
    {
      bool isEnable = true;
      if (dropIsEnable.SelectedValue == "2") isEnable = false;

      if (position != -1)
        gridView.DataSource = bannerData.GetByPosition(position, isEnable);
      else
        gridView.DataSource = bannerData.GetAll(isEnable);
    }
    gridView.DataBind();

    //Bo mui ten len xuong dau va cuoi
    if (gridView.Rows.Count > 0)
    {
      gridView.Rows[0].Cells[7].Enabled = false;
      gridView.Rows[gridView.Rows.Count - 1].Cells[6].Enabled = false;
    }

  }
  private void Enable(bool isEnable)
  {
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          if (check.Checked)
          {
            int id = Convert.ToInt32(item.Cells[2].Text);
            bannerData.Enable(id, isEnable);
          }
          break;
        }
      }
    }
    ShowData();
  }
  protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    try
    {
      if (e.Row.RowIndex >= 0) //Bo qua dong dau
      {
        int id = Convert.ToInt32(e.Row.Cells[2].Text);
        string strPoisiton = e.Row.Cells[4].Text;
        Position position = listPosition.Where(p => p.Id == strPoisiton).FirstOrDefault();
        e.Row.Cells[4].Text = position.Name;

        bool isEnable = bannerData.GetById(id).isEnable;

        //Icon check
        Image img = new Image();
        foreach (Control item in e.Row.Cells[9].Controls)
        {
          if (item.GetType() == typeof(Image))
          {
            img = (Image)item;
            break;
          }
        }
        if (isEnable)
          img.ImageUrl = "~/Images/check.png";
        else
        {
          img.ImageUrl = "~/Images/an.png";

        }
      }
    }
    catch { }
  }
  protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    lbThongBao.Text = "";
    try
    {
      gridView.PageIndex = e.NewPageIndex;
      ShowData();
    }
    catch { }
  }
  protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[2].Text);
      string strImage = bannerData.GetById(id).Image;
      //delete image
      Myfunction.DeleteFile("Images/Banner", strImage);
      bannerData.Delete(id);
      Myfunction.ThongBao(lbThongBao,"Banner đã được xóa");

      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao,ex.Message);
    }
  }
  protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      if (e.CommandName.Equals("Up"))
      {
        int index = Convert.ToInt32(e.CommandArgument);
        int idFirst = Convert.ToInt32(gridView.Rows[index].Cells[2].Text);
        int idSecond = Convert.ToInt32(gridView.Rows[index - 1].Cells[2].Text);

        if (bannerData.GetById(idFirst).Position != bannerData.GetById(idSecond).Position)
        {
          Myfunction.ThongBao(lbThongBao, "Chỉ được đổi thứ tự 2 banner có cùng vị trí");
          return;
        }

        bannerData.SwapSort(idFirst, idSecond);
      }
      else if (e.CommandName.Equals("Down"))
      {
        int index = Convert.ToInt32(e.CommandArgument);
        int idFirst = Convert.ToInt32(gridView.Rows[index].Cells[2].Text);
        int idSecond = Convert.ToInt32(gridView.Rows[index + 1].Cells[2].Text);

        if (bannerData.GetById(idFirst).Position != bannerData.GetById(idSecond).Position)
        {
          Myfunction.ThongBao(lbThongBao, "Chỉ được đổi thứ tự 2 banner có cùng vị trí");
          return;
        }

        bannerData.SwapSort(idFirst, idSecond);
      }
      ShowData();

    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
  protected void btnAdd_Click(object sender, EventArgs e)
  {
    Response.Redirect("~/admin/Banners/Details.aspx");
  }
  protected void btnVisible_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    lbThongBao.Text = "";
    Enable(false);
  }
  protected void btnEnable_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    lbThongBao.Text = "";
    Enable(true);
  }
  protected void btnXoaChon_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      foreach (GridViewRow item in gridView.Rows)
      {
        foreach (Control ctrl in item.Cells[1].Controls)
        {
          if (ctrl.GetType() == typeof(CheckBox))
          {
            CheckBox check = (CheckBox)ctrl;
            if (check.Checked)
            {
              int id = Convert.ToInt32(item.Cells[2].Text);
              //Delete image
              string strImage = bannerData.GetById(id).Image;
              Myfunction.DeleteFile("Images/Banner", strImage);
              bannerData.Delete(id);
            }
            break;
          }
        }
      }
      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao,ex.Message);
    }
  }
  protected void linkCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = true;
          break;
        }
      }
    }
  }
  protected void linkUnCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = false;
          break;
        }
      }
    }
  }
  protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
    {
      lbThongBao.Text = "";
      gridView.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
      ShowData();
    }
  protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void dropPosition_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
}